package com.hue.ps.direct;

import com.hue.utils.ConnectionUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
    发布与订阅模式之direct：生产者
 */
public class Product {
    public static void main(String[] args) throws IOException, TimeoutException {
        //2.获取连接
        Connection connection = ConnectionUtils.getConnection();
        //3.获取频道
        Channel channel = connection.createChannel();

        //4.创建交换机,参数1：交换机名，参数2：交换机类型
        channel.exchangeDeclare("direct_exchange", BuiltinExchangeType.DIRECT);

        //5.声明两个队列
        channel.queueDeclare("direct_queue1", true, false, false, null);
        channel.queueDeclare("direct_queue2", true, false, false, null);

        //6.将某个队列绑定到某个交换机上，并指定routerKey
        channel.queueBind("direct_queue1","direct_exchange", "key1");
        channel.queueBind("direct_queue2","direct_exchange", "key2");

        //5.发送消息,并指定消息的routerKey
        channel.basicPublish("direct_exchange", "key1", null, "router key = key1".getBytes());
        channel.basicPublish("direct_exchange", "key2", null, "router key = key2".getBytes());

        //6.关闭资源
        channel.close();
        connection.close();
    }
}
